package vip.fastgo.retroid.android.gamebox.view.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.util.SparseArray
import android.view.View
import android.widget.Button
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView.ViewHolder

open class BaseViewHolder(itemView: View): ViewHolder(itemView) {

    private val views: SparseArray<View> = SparseArray()

    /**
     * 设置文字
     *
     * @param viewId
     * @param txt
     */
    fun setText(viewId: Int, txt: String) {
        val view = getView<View>(viewId)
        if (view is TextView) {
            view.text = txt
        } else if (view is Button) {
            view.text = txt
        }
    }

    /**
     * 给文字设置颜色
     *
     * @param viewId
     * @param color 颜色
     */
    @SuppressLint("ResourceAsColor")
    fun  setTextColor(viewId: Int, color: Int) {
        var view = getView<View>(viewId)
        if (view is TextView) {
            view.setTextColor(color)
        } else if (view is Button) {
            view.setTextColor(color)
        }
    }

    /**
     * 设置资源图片
     *
     * @param viewId
     * @param resId
     */
    fun setImageRes(viewId: Int, resId: Int) {
        var view = getView<View>(viewId)
        if (view is ImageView) {
            view.setImageResource(resId)
        } else if (view is ImageButton) {
            view.setImageResource(resId)
        }
    }

    /**
     * 设置图片地址
     *
     * @param viewId
     * @param url
     */
//    fun setImageUrl(mContext: Context, viewId: Int, url: String?, tag: Int = -1) {
//        url?.let {
//            var view = getView<View>(viewId)
//            if (view is ImageView) {
//                GlideUtil.loadImage(mContext, url, view, tag)
//            }
//        }
//    }

    /**
     * 给View设置图片
     *
     * @param viewId
     * @param resId 图片资源id
     */
    fun setBgRes(viewId: Int, resId: Int) {
        when (val view = getView<View>(viewId)) {
            is TextView -> {
                view.setBackgroundResource(resId)
            }
            is Button -> {
                view.setBackgroundResource(resId)
            }
            else -> {
                view.setBackgroundResource(resId)
            }
        }
    }

    /**
     * 给View设置图片
     *
     * @param viewId
     * @param color 颜色
     */
    fun setBgColor(viewId: Int, color: Int) {
        when (val view = getView<View>(viewId)) {
            is TextView -> {
                view.setBackgroundColor(color)
            }
            is Button -> {
                view.setBackgroundColor(color)
            }
            else -> {
                view.setBackgroundColor(color)
            }
        }
    }

    /**
     * 显示View
     *
     * @param viewId
     */
    fun showView(viewId: Int) {
        val view = getView<View>(viewId)
        if (View.VISIBLE != view.visibility) {
            view.visibility = View.VISIBLE
        }
    }

    /**
     * 隐藏View
     *
     * @param viewId
     * @param visibility
     */
    fun hideView(viewId: Int, visibility: Int = View.GONE) {
        val view = getView<View>(viewId)
        if (View.VISIBLE == view.visibility) {
            view.visibility = visibility
        }
    }
    /**
     * 通过viewId获取View
     *
     * @param viewId viewId
     * @return view
     */
    fun <T:View> getView(viewId: Int): T {
        var view = views.get(viewId)
        if (view == null) {
            view = itemView.findViewById(viewId)
            views.put(viewId, view)
        }
        return view as T
    }
}